package ganga.custodian.sandbox;

import ganga.custodian.CustodianApplication;
import ganga.custodian.R;
import ganga.custodian.human.HumanModel;
import ganga.custodian.human.PermissibleRange;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * Карточка пациента/настройки модели
 * 
 * @author Cuarto
 * @editor Bacherikov
 * 
 */
public class PatientCard extends Activity {
	private SeekBar maxValue;
	private SeekBar minValue;

	static Context context;
	TextView tv1, tv2;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.patientcard);

		PatientCard.context = getApplicationContext();

		Spinner sensorspeed1 = (Spinner) findViewById(R.id.vegetative);
		ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this,
				R.array.Vegetative, android.R.layout.simple_spinner_item);
		adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sensorspeed1.setAdapter(adapter1);
		sensorspeed1.setOnItemSelectedListener(new myOnItemSelectedListener());

		Spinner sensorspeed2 = (Spinner) findViewById(R.id.hertbeatnum);
		ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this,
				R.array.HeartBeatNum, android.R.layout.simple_spinner_item);
		adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sensorspeed2.setAdapter(adapter2);
		sensorspeed2.setOnItemSelectedListener(new myOnItemSelectedListener());

	}

	public class myOnItemSelectedListener implements OnItemSelectedListener {

		public Spinner mSpinner;
		private String mSelection;
		TextView tv1, tv2;

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			String selected = parent.getItemAtPosition(pos).toString();
			String ds = getString(R.string.vegetativefrompatient);
			tv1 = (TextView) findViewById(R.id.spinner1result);
			// tv2 = (TextView) parent.findViewById(R.id.textView22);
			// tv1.setText(selected);

			SharedPreferences settings = PreferenceManager
					.getDefaultSharedPreferences(context);

			// ���������:
			SharedPreferences.Editor editor = settings.edit();
			editor.putString("pref_key", selected);
			editor.commit();

			// ��������������:
			String myString = settings.getString("pref_key", "");
			tv1.setText(myString);

		}

		public void onNothingSelected(AdapterView parent) {
			// Do nothing.
		}

	}

	private CustodianApplication application;
	private PermissibleRange permissibleRange;

	public void HeartRateButton_Click(View v) {

		maxValue = (SeekBar) findViewById(R.id.maxBar);
		minValue = (SeekBar) findViewById(R.id.minBar);
		Integer maxV, minV;
		maxV = maxValue.getProgress();
		minV = minValue.getProgress();
		application = CustodianApplication.getInstance();
		permissibleRange = application.humanModel.heartRatePermissibleRange;
		permissibleRange.end = minV.doubleValue();
		permissibleRange.start = maxV.doubleValue();

	}
}
